package com.depin.pdaapp.activity;


import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.depin.pdaapp.R;
import com.depin.pdaapp.bean.ProvinceDataBean;
import com.depin.pdaapp.event.MyEvent;
import com.depin.pdaapp.presenter.ZTPresenter;
import com.depin.pdaapp.utils.TimeUtil;
import com.jaydenxiao.common.base.BaseActivity;
import com.jaydenxiao.common.basebean.BaseRespose;
import com.jaydenxiao.common.commonutils.KeyBordUtil;
import com.jaydenxiao.common.commonutils.ToastUitl;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

import static com.uuzuche.lib_zxing.activity.CodeUtils.RESULT_SUCCESS;

public class ZhuTaoActivity extends BaseActivity<ZTPresenter> implements ZTPresenter.View {


    @BindView(R.id.tv_bar)
    TextView tvBar;
    @BindView(R.id.iv_notification)
    ImageView ivNotification;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_time_in)
    TextView tvTimeIn;
    @BindView(R.id.lin_time_in)
    LinearLayout linTimeIn;
    @BindView(R.id.tv_address_in)
    TextView tvAddressIn;
    @BindView(R.id.lin_select_city_in)
    LinearLayout linSelectCityIn;
    @BindView(R.id.et_address_details_in)
    EditText etAddressDetailsIn;
    @BindView(R.id.tv_time_out)
    TextView tvTimeOut;
    @BindView(R.id.tv_wuliu)
    TextView tv_wuliu;
    @BindView(R.id.lin_time_out)
    LinearLayout linTimeOut;
    @BindView(R.id.tv_address_out)
    TextView tvAddressOut;
    @BindView(R.id.lin_select_city_out)
    LinearLayout linSelectCityOut;
    @BindView(R.id.et_address_details_out)
    EditText etAddressDetailsOut;
    @BindView(R.id.et_pici)
    EditText etPici;
    @BindView(R.id.btn_center)
    Button btnCenter;
    private String wuliu;

    @Override
    public int getLayoutId() {
        return R.layout.activity_z_h;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this);
    }

    @Override
    public void initView() {
        tvBar.setText("组套管理");
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
    }

    @OnClick({R.id.iv_back, R.id.lin_time_in, R.id.tv_wuliu, R.id.lin_select_city_in, R.id.lin_time_out, R.id.lin_select_city_out, R.id.btn_center})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_wuliu:
                Intent intent2 = new Intent(this, WuliuActivity.class);
                intent2.putExtra("type", 2);
                startActivityForResult(intent2, 2);
                break;
            case R.id.lin_time_out:
                showTimePickerView(1);
                break;
            case R.id.lin_time_in:
                showTimePickerView(2);
                break;
            case R.id.lin_select_city_in:
                showPickerView(2);
                break;
            case R.id.lin_select_city_out:
                showPickerView(1);
                break;
            case R.id.btn_center:
                Intent intent = new Intent(this, ScanActivity.class);
                startActivityForResult(intent, 1);
                break;
        }
    }

    private void showTimePickerView(int type) {// 弹出选择器
        KeyBordUtil.closeKeybord(this);
        TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                String s = TimeUtil.dateToString(date);
                switch (type) {
                    case 1:
                        tvTimeOut.setText(s);
                        break;
                    case 2:
                        tvTimeIn.setText(s);
                        break;
                }

            }
        })
                .setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确认")//确认按钮文字
                .setTitleSize(16)//标题文字大小
                .setTitleText("时间")//标题文字
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
                .setTitleColor(Color.WHITE)//标题文字颜色
                .setSubmitColor(Color.WHITE)//确定按钮文字颜色
                .setCancelColor(Color.WHITE)//取消按钮文字颜色
                .setTitleBgColor(0xFF0371C6)//标题背景颜色 Night mode
                .setLabel("年", "月", "日", "时", "分", "")//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(true)//是否显示为对话框样式
                .build();
        pvTime.show();
    }

    private void showPickerView(int type) {// 弹出选择器
        KeyBordUtil.closeKeybord(this);
        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String opt1tx = ProvinceDataBean.getInstance().getOptions1Items().size() > 0 ?
                        ProvinceDataBean.getInstance().getOptions1Items().get(options1).getPickerViewText() : "";

                String opt2tx = ProvinceDataBean.getInstance().getOptions2Items().size() > 0
                        && ProvinceDataBean.getInstance().getOptions2Items().get(options1).size() > 0 ?
                        ProvinceDataBean.getInstance().getOptions2Items().get(options1).get(options2) : "";

                String opt3tx = ProvinceDataBean.getInstance().getOptions3Items().size() > 0
                        && ProvinceDataBean.getInstance().getOptions3Items().get(options1).size() > 0
                        && ProvinceDataBean.getInstance().getOptions3Items().get(options1).get(options2).size() > 0 ?
                        ProvinceDataBean.getInstance().getOptions3Items().get(options1).get(options2).get(options3) : "";

                String tx = opt1tx + opt2tx + opt3tx;
                switch (type) {
                    case 1:
                        tvAddressOut.setText(tx);
                        break;
                    case 2:
                        tvAddressIn.setText(tx);
                        break;
                }

            }
        })

                .setTitleText("城市选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .build();
        pvOptions.setPicker(ProvinceDataBean.getInstance().getOptions1Items(), ProvinceDataBean.getInstance().getOptions2Items(), ProvinceDataBean.getInstance().getOptions3Items());//三级选择器
        pvOptions.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == 1 && resultCode == RESULT_SUCCESS) {
            String stringExtra = data.getStringExtra(CodeUtils.RESULT_STRING);
            if (TextUtils.isEmpty(wuliu)) {
                mPresenter.zutao(stringExtra, "", "", "", "", "", "", "", "", tvTimeIn.getText().toString(), tvAddressIn.getText().toString() + etAddressDetailsIn.getText(), tvTimeOut.getText().toString(), tvAddressOut.getText().toString() + etAddressDetailsOut.getText(), etPici.getText().toString(), "");
            } else {
                String[] split = wuliu.split(",");
                mPresenter.zutao(stringExtra, split[0], split[1], split[2], split[3], split[4], split[5], split[6], split[7], tvTimeIn.getText().toString(), tvAddressIn.getText().toString() + etAddressDetailsIn.getText(), tvTimeOut.getText().toString(), tvAddressOut.getText().toString() + etAddressDetailsOut.getText(), etPici.getText().toString(), split[8]);
            }
        } else if (requestCode == 2 && resultCode == 1001) {
            wuliu = data.getStringExtra(CodeUtils.RESULT_STRING);
            tv_wuliu.setText("物流调拨(已填写)");
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsg(MyEvent.MessageEvent event) {
        if (TextUtils.isEmpty(wuliu)) {
            mPresenter.zutao(event.msg, "", "", "", "", "", "", "", "", tvTimeIn.getText().toString(), tvAddressIn.getText().toString() + etAddressDetailsIn.getText(), tvTimeOut.getText().toString(), tvAddressOut.getText().toString() + etAddressDetailsOut.getText(), etPici.getText().toString(), "");
        } else {
            String[] split = wuliu.split(",");
            mPresenter.zutao(event.msg, split[0], split[1], split[2], split[3], split[4], split[5], split[6], split[7], tvTimeIn.getText().toString(), tvAddressIn.getText().toString() + etAddressDetailsIn.getText(), tvTimeOut.getText().toString(), tvAddressOut.getText().toString() + etAddressDetailsOut.getText(), etPici.getText().toString(), split[8]);
        }
    }

    @Override
    public void zutao(BaseRespose baseRespose) {
        ToastUitl.showShort(baseRespose.getMsg());

    }
}